package edu.wit.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.wit.common.db.service.impl.BaseServiceImpl;
import edu.wit.common.security.service.TokenService;
import edu.wit.common.utils.ServletUtils;
import edu.wit.common.security.config.TokenManager;
import edu.wit.system.domain.AffairsApplyWorkflow;
import edu.wit.system.domain.AffairsApplyWorkflowCompany;
import edu.wit.system.domain.User;
import edu.wit.system.mapper.AffairsApplyWorkflowCompanyMapper;
import edu.wit.system.mapper.AffairsApplyWorkflowMapper;
import edu.wit.system.service.AffairsApplyWorkflowCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AffairsApplyWorkflowCompanyServiceImpl extends BaseServiceImpl<AffairsApplyWorkflowCompanyMapper, AffairsApplyWorkflowCompany> implements AffairsApplyWorkflowCompanyService {

    @Autowired
    AffairsApplyWorkflowMapper affairsApplyWorkflowMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TokenManager tokenManager;
    
    @Autowired
    private TokenService tokenService;

    @Override
    public List<AffairsApplyWorkflowCompany> selectByCompanyId(String companyId) {
        List<AffairsApplyWorkflowCompany> affairsApplyWorkflowCompanyList = baseMapper.selectList(new QueryWrapper<AffairsApplyWorkflowCompany>().eq("company_id",companyId));
        affairsApplyWorkflowCompanyList.forEach(affairsApplyWorkflowCompany -> {
            AffairsApplyWorkflow affairsApplyWorkflow=affairsApplyWorkflowMapper.selectOne(new QueryWrapper<AffairsApplyWorkflow>().eq("apply_id",affairsApplyWorkflowCompany.getAffairsApplyWorkflowId()));
            affairsApplyWorkflowCompany.setAffairsApplyWorkflowStatus(affairsApplyWorkflow.getApplyStatus());
        });
        return affairsApplyWorkflowCompanyList;
    }

    @Override
    public List<AffairsApplyWorkflowCompany> selectByCompanyId() {
//        String username = redisTemplate.opsForValue().get(ServletUtils.getRequest().getHeader("token")).toString();
//        String token = ServletUtils.getRequest().getHeader("token");
//        SysUser sysUser = tokenService.getLoginUser(token).getSysUser();

        String username = ServletUtils.getRequest().getHeader("username");

//        User user= affairsApplyWorkflowMapper.selectUserByUsername(username);
        User user = affairsApplyWorkflowMapper.selectUserByUsername(username);

        String projectAdminId = user.getPid().equals("0") ? user.getId() :user.getPid();
        List<AffairsApplyWorkflowCompany> affairsApplyWorkflowCompanyList = baseMapper.selectList(new QueryWrapper<AffairsApplyWorkflowCompany>().eq("project_admin_id",projectAdminId));
        affairsApplyWorkflowCompanyList.forEach(affairsApplyWorkflowCompany -> {
            AffairsApplyWorkflow affairsApplyWorkflow=affairsApplyWorkflowMapper.selectOne(new QueryWrapper<AffairsApplyWorkflow>().eq("apply_id",affairsApplyWorkflowCompany.getAffairsApplyWorkflowId()));
            affairsApplyWorkflowCompany.setAffairsApplyWorkflowStatus(affairsApplyWorkflow.getApplyStatus());
        });
        return affairsApplyWorkflowCompanyList;
    }
}
